window.addEventListener('load', function () {

    //手机发送5s模块
    let code = document.querySelector('.code');

    let i = 5;
    flag = true;
    code.addEventListener('click', function () {
        if (flag) {
            flag = false;
            gettimer();
            function gettimer() {
                code.innerHTML = `0${i}秒后重新获取`
                i--;
                if (i < 0) {
                    i = 5;
                    clearInterval(timer);
                    code.innerHTML = `重新获取`;
                    flag = true;
                    // console.log(i);
                }

            }
            let timer = setInterval(gettimer, 1000)
        }
    })
    //用户名验证码模块
    let uname = document.querySelector('[name=username]');
    let msg = uname.nextElementSibling;
    function veifyName() {
        // let str = uname.value;
        let reg = /^[a-zA-Z0-9]{6,10}$/
        if (!reg.test(uname.value)) {

            msg.innerHTML = '输入不合法,请输入6-10位';
            return false;

        }
        msg.innerHTML = '';
        return true;

    }
    uname.addEventListener('change', veifyName);
    // console.log(uname);

    //手机号码模块
    let phone = document.querySelector('[name=phone]');
    let phonemsg = phone.nextElementSibling;
    function veifyphone() {
        // let str = uname.value;
        let reg = /^1(3\d|4[5-9]|5[0-35-9]|6[567]7[0-8]|8\d|9[0-35-9])\d{8}$/;
        if (!reg.test(phone.value)) {
            phonemsg.innerHTML = '输入不合法,请输正确的手机号码';
            return false;
        }
        phonemsg.innerHTML = '';
        return true;

    }
    phone.addEventListener('change', veifyphone);
    // console.log(uname);

    //验证码模块
    let phonecode = document.querySelector('[name=code]');
    let phonecodemsg = phonecode.nextElementSibling;
    function veifyphonecode() {
        // let str = uname.value;
        let reg = /^\d{6}$/;
        if (!reg.test(phonecode.value)) {
            phonecodemsg.innerHTML = '请输正确的验证码';
            return false;
        }
        phonecodemsg.innerHTML = '';
        return true;

    }
    phonecode.addEventListener('change', veifyphonecode);
    // console.log(uname);

    //密码模块
    let password = document.querySelector('[name=password]');
    let passwordmsg = password.nextElementSibling;
    function veifypassword() {
        // let str = uname.value;
        let reg = /^[a-zA-Z0-9]{6,20}$/;
        if (!reg.test(password.value)) {
            passwordmsg.innerHTML = '请输正确的密码';
            return false;
        }
        passwordmsg.innerHTML = '';
        return true;

    }
    password.addEventListener('change', veifypassword);
    // console.log(uname);

    //密码验证模块
    let confirm = document.querySelector('[name=confirm]');
    let confirmmsg = confirm.nextElementSibling;
    function veifyconfirm() {
        // let str = uname.value;

        if (confirm.value !== password.value) {
            confirmmsg.innerHTML = '请输正确的密码';
            return false;
        }
        confirmmsg.innerHTML = '';
        console.log(1);
        return true;

    }
    confirm.addEventListener('change', veifyconfirm);
    //确认模块
    let queren = document.querySelector('.icon-queren');
    queren.addEventListener('click', function () {
        queren.classList.toggle('icon-queren2');
        // console.log(queren.classList.contains('icon-queren2'));//一定要写点击事件里
        console.log(veifyName());
    })
    //提交模块

    let form = document.querySelector('form');
    form.addEventListener('submit', function (e) {
        if (!queren.classList.contains('icon-queren2')) {
            e.preventDefault();
            alert('请你勾选用户协议');
        }
        // if (!veifyName()) e.preventDefault(); alert('请输入正确的数据');
        if (!veifyName() || !veifyphone() || !veifyphone() || !veifyphonecode() || !veifypassword() || !veifyconfirm()) {
            e.preventDefault();
            alert('数据不能为空或是错误');
        }

    })




})
